package com.fashion.server.service.impl;

import com.fashion.server.utils.JsonUtil;
import com.fashion.server.domain.model.IdentityOfficialModel;
import com.fashion.server.domain.model.PunishTypeModel;
import com.fashion.server.domain.request.HttpDataParam;
import com.fashion.server.domain.response.ResponseData;
import com.fashion.server.common.IConstants;
import com.fashion.server.common.IConstants.ResponseDefine.Code;
import com.fashion.server.dao.MapperFactory;
import com.fashion.server.service.IdentityOfficialService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("identityOfficialService")
public class IdentityOfficialServiceImpl implements IdentityOfficialService{
	
    private static final Logger logger = Logger.getLogger(IdentityOfficialServiceImpl.class);

    @Resource
    private MapperFactory mapperFactory;
    
	@Override
	public String queryIdentityOfficial(IdentityOfficialModel model) {
		 //只查询count数
        if(model.onlyQueryCount()){
            ResponseData responseData = new ResponseData();
            responseData.setCode(IConstants.ResponseDefine.Code.SUCCESS_200.getIndex());
            responseData.setData(mapperFactory.getReadIdentityOfficialMapper().selectCountByObject(model));
            return JsonUtil.toJSONString(responseData, false);
        }

        ResponseData responseData = new ResponseData();

        //是否需要返回totalCount
        if(model.needTotalCount()){
            responseData.setTotalCount(mapperFactory.getReadIdentityOfficialMapper().selectCountByObject(model));
        }
        List<IdentityOfficialModel> list = mapperFactory.getReadIdentityOfficialMapper().selectByObejctPaging(model);
        if (list.isEmpty()) {
            responseData.setCode(IConstants.ResponseDefine.Code.SUCCESS_200.getIndex());
            responseData.setMsg("没有查询到数据");
            responseData.setData(new ArrayList<PunishTypeModel>());
        } else {
            responseData.setData(list);
        }
        return JsonUtil.toJSONString(responseData, false);
	}
	
	 @Override
	 public String addIdentityOfficial(HttpDataParam<IdentityOfficialModel> dataCondition) {
			List<IdentityOfficialModel> list = dataCondition.getObjects();
			ResponseData responseData = new ResponseData();
			StringBuffer ids = new StringBuffer();
			int count = 0;
			if(null != list){
				//参数校验
				for(IdentityOfficialModel model : list){
					ResponseData checkData = checkAddData(model);
					checkData.setData(model);
					if (Code.SUCCESS_200.getIndex() != checkData.getCode()) {
						return JsonUtil.toJSONString(checkData, false);
					}
				}
				
			    for (IdentityOfficialModel model : list) {
			    	int ret =  mapperFactory.getWriteIdentityOfficialMapper().insertSelective(model);
			    	count = count + ret;
			    	ids.append(model.getId()).append(",");
					logger.info("新增官方认证信息id:["+model.getId()+"]");
			    }
			    ids.deleteCharAt(ids.length() - 1);
			    Map<String,Object> map = new HashMap<String,Object>();
			    map.put("count", count);
			    map.put("ids", ids);
			    responseData.setData(map);
			}else{
			    responseData.setCode(IConstants.ResponseDefine.Code.ERROR_402.getIndex());
			    responseData.setMsg("参数为空");
			}
			
			return JsonUtil.toJSONString(responseData, false);
	    }
	    
		private ResponseData checkAddData(IdentityOfficialModel object) {
			ResponseData responseData = new ResponseData();
			responseData.setCode(Code.ERROR_402.getIndex());
			responseData.setData(object);
			if (StringUtils.isEmpty(object.getName())) {
				responseData.setMsg("参数错误:name[官方认证身份]不能为空");
				return responseData;
			}
			return new ResponseData(Code.SUCCESS_200.getIndex());
		}
		
		private ResponseData checkUpdateData(IdentityOfficialModel object) {
			ResponseData responseData = new ResponseData();
			responseData.setCode(Code.ERROR_402.getIndex());
			responseData.setData(object);
			if (null == object.getId()) {
				responseData.setMsg("参数错误:Id不能为空");
				return responseData;
			}
			if (null != object.getName() && "".equals(object.getName().trim())) {
				responseData.setMsg("参数错误:name[官方认证身份]不能为空");
				return responseData;
			}
			return new ResponseData(Code.SUCCESS_200.getIndex());
		} 

	    @Override
	    public String updateIdentityOfficial(HttpDataParam<IdentityOfficialModel> dataCondition) {
	    	int count = 0;
	        List<IdentityOfficialModel> list = dataCondition.getObjects();
	        ResponseData responseData = new ResponseData();
	        if(null != list){
	        	//参数校验
	        	for(IdentityOfficialModel model : list){
	        		ResponseData checkData = checkUpdateData(model);
	        		checkData.setData(model);
	    			if (Code.SUCCESS_200.getIndex() != checkData.getCode()) {
	    				return JsonUtil.toJSONString(checkData, false);
	    			}
	        	}
	            for (IdentityOfficialModel model : list) {
	                int ret = mapperFactory.getWriteIdentityOfficialMapper().updateByPrimaryKeySelective(model);
	                count = count + ret;
	                logger.info("更新官方认证,id=" + model.getId());
	            }
	            Map<String,Object> map = new HashMap<String,Object>();
	            map.put("count", count);
	            responseData.setData(map);
	        }else{
	            responseData.setCode(IConstants.ResponseDefine.Code.ERROR_402.getIndex());
	            responseData.setMsg("参数为空");
	        }

	        return JsonUtil.toJSONString(responseData, false);
	    }

}
